package com.imooc.mall.service.impl;

import com.imooc.mall.common.Constant;
import com.imooc.mall.exception.ImoocMallException;
import com.imooc.mall.exception.ImoocMallExceptionEnum;
import com.imooc.mall.model.dao.CartMapper;
import com.imooc.mall.model.dao.CategoryMapper;
import com.imooc.mall.model.dao.ProductMapper;
import com.imooc.mall.model.pojo.Cart;
import com.imooc.mall.model.pojo.Product;
import com.imooc.mall.service.CartService;
import com.imooc.mall.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private CartMapper cartMapper;


    /**
     * 展示购物车功能
     * @param UserId
     * @return
     */
    @Override
    public List<CartVO> list(Integer UserId){
        List<CartVO> cartVOS = cartMapper.selectList(UserId);
        for (int i = 0; i < cartVOS.size(); i++) {
            CartVO cartVO = cartVOS.get(i);
            cartVO.setTotalPrice(cartVO.getPrice()*cartVO.getQuantity());
        }
        return cartVOS;
    }

    @Override
    public List<CartVO> add(Integer userId, Integer productId, Integer count){
        //先判断该次请求的合法性
        validProduct( productId, count);
        //查询购物车是否存在商品
        Cart cart = cartMapper.selectCartByUserIdAndProductId(userId, productId);
        if (cart == null){
            //购物车没有商品
            cart = new Cart();
            cart.setUserId(userId);
            cart.setQuantity(count);
            cart.setProductId(productId);
            cart.setCreateTime(new Date());
            cart.setSelected(Constant.Cart.CHECKED);
            cartMapper.insertSelective(cart);
        }else {
            //购物车存在该商品，只需要更改商品的数量
            count = cart.getQuantity() + count;
            Cart cartNew = new Cart();
            cartNew.setQuantity(count);
            cartNew.setId(cart.getId());
            cartNew.setProductId(cart.getProductId());
            cartNew.setUserId(cart.getUserId());
            cartNew.setSelected(Constant.Cart.CHECKED);
            cartMapper.updateByPrimaryKeySelective(cartNew);
        }
return this.list(userId);
    }

    /**
     * 更新购物车功能
     * @param userId
     * @param productId
     * @param count
     * @return
     */
    @Override
   public List<CartVO> update(Integer userId, Integer productId, Integer count){
        //依旧是现对id进行校验
       validProduct(productId,count);
        Cart cart = cartMapper.selectCartByUserIdAndProductId(userId, productId);
       if (cart==null){
           //这个商品不在购物车
           throw new ImoocMallException(ImoocMallExceptionEnum.UPDATE_FAILED);
       }else{
           Cart cartNew = new Cart();
           cartNew.setQuantity(count);
           cartNew.setId(cart.getId());
           cartNew.setProductId(cart.getProductId());
           cartNew.setUserId(cart.getUserId());
           cartNew.setSelected(Constant.Cart.CHECKED);
           cartMapper.updateByPrimaryKeySelective(cartNew);
       }
       return this.list(userId);
   }

    /**
     * 删除购物车
     * @param productId
     * @param userId
     * @return
     */
    @Override
    public List<CartVO> delete(Integer productId, Integer userId){
        //现根据id、查出该商品
        Cart cart = cartMapper.selectCartByUserIdAndProductId(userId, productId);
        if (cart==null){
            //该商品不在购物车内
            throw new ImoocMallException(ImoocMallExceptionEnum.DELETE_FAILED);
        }else {
            //调用删除方法
            cartMapper.deleteByPrimaryKey(cart.getId());
        }
        return this.list(userId);
    }


    private void validProduct(Integer productId, Integer count) {
        //先判断商品是否存在并且是否处于上架状态
        Product product = productMapper.selectByPrimaryKey(productId);
        if (product==null || !product.getStatus().equals(Constant.SaleStatus.SALT)){
            throw new ImoocMallException(ImoocMallExceptionEnum.NOT_SALE);
        }
        //再判断商品的库存是否足够
        if (count > product.getStock()){
            //库存不足
            throw new ImoocMallException(ImoocMallExceptionEnum.NOT_ENOUGH);
        }
    }

    /**
     * 选择购物车一个多个或全部商品
     */
    @Override
    public List<CartVO> selectOrNot(Integer userId, Integer productId, Integer selected){
        Cart cart = cartMapper.selectCartByUserIdAndProductId(userId, productId);
        if (cart == null){
            //购物车没有商品
            throw new ImoocMallException(ImoocMallExceptionEnum.UPDATE_FAILED);
        }else {
            //购物车聪仔商品，可以使用
            cartMapper.selectOrNot(userId,productId,selected);
        }
        return this.list(userId);
    }

    @Override
    public List<CartVO> selctAllOrNot(Integer userId, Integer selected){
        cartMapper.selectOrNot(userId,null,selected);
        return this.list(userId);
    }


}
